#include "fig_ui_yesno.h"
#include "gen_game_state.h"
#include "fig_sounds.h"

#include "gameflow/ui_popup.h"

FIGUIYesNo::FIGUIYesNo(gameflow::UIPopup* pPopup)  : 
							FIGUIPopupAnimator(pPopup),
							m_IsShowingAnimation(false)
{
	
	
	if(m_pPopup)
	{
		SetCloseButton("NoWidget");
		m_pPopup->SetWidgetFunctor("NoWidget", UI_DELEGATE(FIGUIYesNo, OnButtonClose));
		AddToNaviationUI(m_pPopup->GetWidget("NoWidget"));
		AddToNaviationUI(m_pPopup->GetWidget("YesWidget"));
		FIGUIPopupAnimatorMgr::Instance().AddPopupAnimator(FIGUIPopupAnimatorMgr::POPUP_NAMES_YESNO,this);


		m_PosMsgBox = m_pPopup->GetWidget("BG")->GetPosition();
		m_PosYes = m_pPopup->GetWidget("YesWidget")->GetPosition();
		m_PosNo = m_pPopup->GetWidget("NoWidget")->GetPosition();
		

	}
	m_Type = ANIMATION_MSGPOPUP;

}

void		FIGUIYesNo::Hide()
{
	if(GENGameState::GetGame())
		GENGameState::GetGame()->UnPause();
	audio::BassAudio::Instance().RestartAll();

	FIGUIPopupAnimator::Hide();
}


bool	FIGUIYesNo::OnUIKeyDownEvent(int	KeyCode)
{
	int NewKeyCode = KeyCode;
	
	if(KeyCode == input::MOGAJoystick::KEYCODE_DPAD_LEFT)
		NewKeyCode = input::MOGAJoystick::KEYCODE_DPAD_UP;

	else if(KeyCode == input::MOGAJoystick::KEYCODE_DPAD_RIGHT)
		NewKeyCode = input::MOGAJoystick::KEYCODE_DPAD_DOWN;
	
	return FIGUIPopupAnimator::OnUIKeyDownEvent(NewKeyCode);

}

void DELEGATE_CALL		FIGUIYesNo::OnButtonClose(ui::UIWidget::WIDGET_EVENT Event, ui::UIWidget* pWidget)
{
	this->Hide();
}

void	FIGUIYesNo::Tick(float dt)
{
	FIGUIPopupAnimator::Tick(dt);
	if(!m_IsShowingAnimation)
		return;


	bool b1 = false;
	bool b2 = false;
	bool b3 = false;

	const float SPEED = 1.0f;

	math::Vec2 TempPos;
	TempPos = m_pPopup->GetWidget("BG")->GetPosition();
	TempPos.y += (SPEED * dt);
	if(TempPos.y >= m_PosMsgBox.y)
	{
		TempPos.y = m_PosMsgBox.y;
		b1 = true;
	}
	m_pPopup->GetWidget("BG")->SetPositionRelativeToParent(TempPos);

	TempPos = m_pPopup->GetWidget("YesWidget")->GetPosition();
	TempPos.x += (SPEED * dt);
	if(TempPos.x >= m_PosYes.x)
	{
		TempPos.x = m_PosYes.x;
		b2 = true;
	}
	m_pPopup->GetWidget("YesWidget")->SetPositionRelativeToParent(TempPos);

	TempPos = m_pPopup->GetWidget("NoWidget")->GetPosition();
	TempPos.x += (-SPEED * dt);
	if(TempPos.x <= m_PosNo.x)
	{
		TempPos.x = m_PosNo.x;
		b3 = true;
	}
	m_pPopup->GetWidget("NoWidget")->SetPositionRelativeToParent(TempPos);

	if(b1 && b2 && b3)
		m_IsShowingAnimation = false;




}

void	FIGUIYesNo::Show()
{
	m_IsShowingAnimation = true;

	m_pPopup->GetWidget("BG")->SetPositionRelativeToParent (m_PosMsgBox + math::Vec2(0,-0.1f));
	m_pPopup->GetWidget("YesWidget")->SetPositionRelativeToParent(m_PosYes + math::Vec2(-0.1f,0));
	m_pPopup->GetWidget("NoWidget")->SetPositionRelativeToParent(m_PosNo + math::Vec2(0.1f,0));

	m_pPopup->GetWidget("NoWidget")->SetTextA("");
	m_pPopup->GetWidget("YesWidget")->SetTextA("");

	FIGUIPopupAnimator::Show();
	Tick(0);

}
	